package Part5_转换流;

import org.junit.Test;

import java.io.*;

/**
 * ClassName: StreamTest
 * Package: IntelliJ IDEA
 * Description:
 * Written by HanXu
 * Create 2024.04.22 22:00
 */
public class StreamTest {
    @Test
    public void test1() throws IOException{
        //1、造文件
        File file1 = new File("infoInput.txt");
        File file2 = new File("infoOutput.txt");

        //2、创建字符流
        FileInputStream fis = new FileInputStream(file1);
        FileOutputStream fos = new FileOutputStream(file2);

        //3、创建转换流
        //解码集：与传入文件保持一致
        InputStreamReader isr = new InputStreamReader(fis,"utf8");
        //编码集：指明存储文件的编码类型
        OutputStreamWriter osw = new OutputStreamWriter(fos,"GBK");

        //4、转换操作
        char[] cBuffer = new char[1024];
        int len;
        while ((len=isr.read(cBuffer))!=-1){
            osw.write(cBuffer,0 ,len);
        }

        //5、关闭资源
        osw.close();
        isr.close();
    }
}
